package com.wmyg.antifake.controller;

import com.wmyg.antifake.dto.QueryDto;
import com.wmyg.antifake.service.QueryService;
import com.wmyg.antifake.vo.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-12
 */
@Slf4j
@RestController
public class QueryController {

    @Autowired
    QueryService queryService;

    @GetMapping("/trackinfo")
    public Message track(QueryDto queryDto, HttpServletRequest request) {
        String userAgent = request.getHeader("user-agent");
        String clientIp = request.getHeader("client-ip");
        queryDto.setUserAgent(userAgent);
        queryDto.setClientIp(clientIp);
        queryDto.setQueryTime(new Date());
        log.info("Recieve new query request {}", queryDto);
        if ("QR_CODE".equals(queryDto.getType())) {
            return queryService.trackQrCode(queryDto);
        } else if ("EAN_13".equals(queryDto.getType())){
            return queryService.trackEanCode(queryDto);
        }
        return null;
    }

    @GetMapping("/ismatch")
    public Boolean isMatch(String qr1, String qr2) {
        return queryService.match(qr1, qr2);
    }

}
